package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.FriendDto;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.GroupMemberDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.chat.ChatMessageDto;
import com.lanjiu.lib.imclient.dto.chat.TextDto;
import com.lanjiu.lib.imclient.dto.extra.ChatFriendMsgDto;
import com.lanjiu.lib.imclient.dto.extra.ChatGroupMsgDto;
import com.lanjiu.lib.imclient.dto.extra.InviteDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.GUID;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/2 20:31
 */
public class ApplyFriendView extends IView {

    private ApplyFriendCallback mCallback;

    public ApplyFriendView(ApplyFriendCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doRefuseFriend(String friendUserId, String friendNick, String friendAvatar, String userType, String levelMessage) {
        ChatFriendMsgDto chatFriendMsgDto = new ChatFriendMsgDto();
        FriendDto friendDto = new FriendDto(UserPreferences.isRegistered() ? IMSContacts.UserType.REGISTERED_USER : IMSContacts.UserType.TOURISTS_USER);
        friendDto.setFriendUserId(friendUserId);
        friendDto.setFriendNickname(friendNick);
        friendDto.setFriendAvatar(friendAvatar);
        friendDto.setUserType(userType);
        chatFriendMsgDto.setFriendDto(friendDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        chatMessageDto.setChatType(UserPreferences.isRegistered() ? IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_REGISTERED : IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS);
        chatMessageDto.setMsgId(GUID.random());
        TextDto textDto = new TextDto();
        textDto.setContent(levelMessage);
        textDto.setLength(String.valueOf(levelMessage.length()));
        chatMessageDto.setText(textDto);
        chatFriendMsgDto.setChatMessageDto(chatMessageDto);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_APPLY_REFUSE_REGISTERED : EventConstants.Action.FRIEND_APPLY_REFUSE_TOURISTS;
        doSendRequest(action, chatFriendMsgDto.toJson());
    }

    public void doAgreeFriend(String friendUserId, String friendNick, String friendAvatar, String userType) {
        FriendDto friendDto = new FriendDto(UserPreferences.isRegistered() ? IMSContacts.UserType.REGISTERED_USER : IMSContacts.UserType.TOURISTS_USER);
        friendDto.setFriendUserId(friendUserId);
        friendDto.setFriendNickname(friendNick);
        friendDto.setFriendAvatar(friendAvatar);
        friendDto.setUserType(userType);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_APPLY_AGREE_REGISTERED : EventConstants.Action.FRIEND_APPLY_AGREE_TOURISTS;
        doSendRequest(action, friendDto.toJson());
    }

    public void doRefuseGroup(String groupId, String groupName, String memberUid, String levelMessage) {
        ChatGroupMsgDto chatGroupMsgDto = new ChatGroupMsgDto();
        GroupMemberDto groupMemberDto = new GroupMemberDto(UserPreferences.getUserType());
        groupMemberDto.setGroupMemberUserId(UserPreferences.getUserId());
        groupMemberDto.setGroupNemberName(UserPreferences.getNickname());
        groupMemberDto.setGroupMemberAvatar(UserPreferences.getAvatar());
        GroupDto groupDto = new GroupDto(UserPreferences.getUserType());
        groupDto.setGroupId(groupId);
        groupDto.setGroupName(groupName);
        groupDto.setGroupType(IMSUtils.userTypeAnalysis(groupId));
        groupDto.setGroupOwnerUserId(memberUid);
        groupDto.setSnapchatStatus(false);
        groupMemberDto.setGroup(groupDto);
        chatGroupMsgDto.setGroupMemberDto(groupMemberDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        chatMessageDto.setChatType(UserPreferences.isRegistered() ? IMSContacts.MsgContentType.APPLY_REFUSE_REGISTERED : IMSContacts.MsgContentType.APPLY_REFUSE_TOURISTS);
        chatMessageDto.setMsgId(GUID.random());
        TextDto textDto = new TextDto();
        textDto.setContent(levelMessage);
        textDto.setLength(String.valueOf(levelMessage.length()));
        chatMessageDto.setText(textDto);
        chatGroupMsgDto.setChatMessageDto(chatMessageDto);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_MEMBER_APPLY_REFUSE_REGISTERED : EventConstants.Action.GROUP_MEMBER_APPLY_REFUSE_TOURISTS;
        doSendRequest(action, chatGroupMsgDto.toJson());
    }

    public void doAgreeGroup(String groupId, String userId) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_MEMBER_INVITE_REGISTERED : EventConstants.Action.GROUP_MEMBER_INVITE_TOURISTS;
        doSendRequest(action, new InviteDto(groupId, userId).toJson());
    }

    @Override
    protected void onMessageEventHandle(MessageEvent event) {
        Logger.print("ApplyFriendView", event.toString());
        if (event.action() == EventConstants.Action.FRIEND_APPLY_REFUSE_TOURISTS
                || event.action() == EventConstants.Action.FRIEND_APPLY_REFUSE_REGISTERED) {
            if (null != mCallback)
                mCallback.onRefuseCallback(event.code(), event.msg());
        } else if (event.action() == EventConstants.Action.FRIEND_APPLY_AGREE_TOURISTS
                || event.action() == EventConstants.Action.FRIEND_APPLY_AGREE_REGISTERED) {
            FriendDto friendDto = null;
            if (checkIsSuccess(event.code(), event.data()))
                friendDto = Dto.fromJson(event.data(), FriendDto.class);
            if (null != mCallback)
                mCallback.onAgreeCallback(event.code(), event.msg(), friendDto);
        } else if (event.action() == EventConstants.Action.GROUP_MEMBER_APPLY_REFUSE_TOURISTS
                || event.action() == EventConstants.Action.GROUP_MEMBER_APPLY_REFUSE_REGISTERED) {
            if (null != mCallback)
                mCallback.onGroupRefuseCallback(event.code(), event.msg());
        } else if (event.action() == EventConstants.Action.GROUP_MEMBER_INVITE_TOURISTS
                || event.action() == EventConstants.Action.GROUP_MEMBER_INVITE_REGISTERED) {
            if (null != mCallback)
                mCallback.onGroupAgreeCallback(event.code(), event.msg());
        }
    }

    public interface ApplyFriendCallback {

        void onAgreeCallback(int code, String msg, FriendDto data);

        void onRefuseCallback(int code, String msg);

        void onGroupAgreeCallback(int code, String msg);

        void onGroupRefuseCallback(int code, String msg);
    }
}
